package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishDtoMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.service.DishDtoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class DishDtoServiceImpl implements DishDtoService {

    @Autowired(required = false)
    private DishDtoMapper dishDtoMapper;
    @Autowired(required = false)
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryServiceImpl categoryService;
    @Autowired (required = false)
    private CategoryMapper categoryMapper;
    @Autowired
    private RedisTemplate redisTemplate;



    /**
     * 添加菜品
     *
     * @param
     * @return
     */
    @Override
    public Boolean addDishDto(DishDto dishDto) {

        dishDtoMapper.addDish(dishDto);
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return true;
    }

    /**
     * 根据菜品名字查找菜品
     *
     * @param
     * @return
     */
    @Override
    public Dish selectByName(String name) {
        return dishDtoMapper.selectByName(name);
    }

    /**
     * 菜品分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<DishDto> findByPage(Integer page, Integer pageSize, String name) {
        //设置当前页和每页大小
        PageHelper.startPage(page,pageSize);
        //调用mapper层完成查询
        List<DishDto> dishDtoList = dishDtoMapper.findAllByName(name);
        //封住PageInfo
        PageInfo<DishDto> dishDtoPageInfo = new PageInfo<>(dishDtoList);
        //封装Page
        Page<DishDto> dishDtoPage = new Page<>();
        //获取菜品名字,并且赋值给DishDto中的CategoryName
        for (DishDto dishDto : dishDtoList) {
           //通过分类id获取分类名
            Category category = categoryMapper.findCategoryById(dishDto.getCategoryId());
            dishDto.setCategoryName(category.getName());
        }
        dishDtoPage.setRecords(dishDtoPageInfo.getList());
        dishDtoPage.setTotal(dishDtoPageInfo.getTotal());
        return dishDtoPage ;
    }

    /**
     * 根据id查找菜品
     *
     * @param id
     * @return
     */
    @Override
    public DishDto findDishDtoById(Long id) {
        //查找dishDto
        DishDto dishDto= dishDtoMapper.findDishDtoById(id);
        //查找flavor
        List<DishFlavor> dishFlavorList = dishFlavorMapper.findDishFlavorById(id);
        dishDto.setFlavors(dishFlavorList);

        return dishDto;
    }

    /**
     * 菜品修改
     *
     * @param dishDto
     * @return
     */
    @Override
    public Boolean update(DishDto dishDto) {
        //1.修改Dish
        Integer dish = dishDtoMapper.updateDishDto(dishDto);
        //2.获取口味列表
        List<DishFlavor> dishFlavorList = dishDto.getFlavors();
        //3.修改DishFlavor
        Integer dishFlavor = dishFlavorMapper.updateDishFlavor(dishFlavorList);
        //4.清空缓存
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return true;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public Boolean deleteByIds(Long[] ids) {
        Integer dishDto = dishDtoMapper.DeleteByIds(ids);
        Integer dishFlavor = dishFlavorMapper.DeleteByIds(ids);

        //4.清空缓存
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        return dishDto>0&&dishFlavor>0;
    }

    /**
     * 批量禁用
     *
     * @param ids
     * @return
     */
    @Override
    public Boolean unUse(Long[] ids) {

        return dishDtoMapper.unUse(ids)>0;
    }

    /**
     * 批量启用
     *
     * @param ids
     * @return
     */
    @Override
    public Boolean use(Long[] ids) {
        return dishDtoMapper.use(ids)>0;
    }

    /**
     * 根据菜品分类id查找菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<DishDto> findDishDtoByCategoryId(Long categoryId,Integer status) {

        //1.从缓存获取数据

        List<DishDto> dishDtoList = (List<DishDto>) redisTemplate.opsForValue().get("dish_"+categoryId+"_"+status);
        //List<DishDto> dishDtoList =  redisTemplate.opsForList().range("list_"+categoryId+"_"+status,0,-1);
        //2.获取不到
        if (dishDtoList==null||dishDtoList.size()==0){
            //2.1调用数据库获取
             dishDtoList = dishDtoMapper.findDishDtoByCategoryId(categoryId,status);
            for (DishDto dishDto : dishDtoList) {
                Long dishId = dishDto.getId();
                List<DishFlavor> dishFlavorList = dishFlavorMapper.findDishFlavorById(dishId);
                dishDto.setFlavors(dishFlavorList);
            }
            //2.2写入缓存,缓存时间60*24
            redisTemplate.opsForValue().set("dish_"+categoryId+"_"+status,dishDtoList,60*24, TimeUnit.MINUTES);
           //redisTemplate.opsForList().leftPushAll("list_"+categoryId+"_"+status,dishDtoList);
        }

        return dishDtoList ;
    }
}
